package com.manage.demo.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.manage.demo.dao.model.TeacherInfo;
import com.manage.demo.dao.model.CourseInfo;

@Mapper
public interface teacherMapper {
	@Select("select "
			+ "a.*,b.* "
			+ "from teacher_info as a inner join course_info as b "
			+ "on a.subject=b.course_id "
			+ "where "
			+ "(#{id}='' or a.tch_id=#{id}) "
			+ "and (#{subject}='' or a.subject=#{subject}) "
			+ "order by a.tch_id "
			+ "limit #{limit} "
			+ "offset #{offset} ")
	public List<Map<String, Object>> getTeacherListByCondition(String subject, String id, int limit, int offset);

	@Select("select Administrator from user_info where user_id=#{id}")
	public Integer getAdministratorByCondition(String id);
	
	@Select("select * from teacher_info where tch_id=#{id}")
	public List<Map<String, Object>> getTeacherMange(String id);
	
	@Select("select tch_id from teacher_info where `tch_id` LIKE concat('%', #{id}, '%')")
	public List<Integer> getTeacherIdByCondition(Integer id);

	@Insert({
		"<script> ",
		"insert into teacher_info ",
		"(tch_id,name,sex,tele,subject,create_time,update_time) ",
		"values ",
		"(#{newid},#{name},#{sex},#{tel},#{subject},now(),now())",
		"</script>"
	})
	public Integer setTeacherByCondition(String newid, String name, String sex, String tel, String subject);
	
	@Insert({
		"<script> ",
		"insert into user_info",
		"(user_id,password,permission,Administrator,create_time,update_time) ",
		"values ",
		"(#{newid},#{newid},1,#{administrator},now(),now())",
		"</script>"
	})
	public Integer setUserByCondition(String newid, String administrator);
	
	@Update("update teacher_info set name=#{name}, sex=#{sex}, tele=#{tel}, subject=#{subject} where tch_id=#{id} ")
	public Integer updateTeacher(String id, String name, String sex, String tel, String subject);
	
	@Update("update user_info set Administrator=#{administrator} where user_id=#{id} ")
	public Integer updateUser(String id, String administrator);

	@Update("delete from teacher_info where tch_id=#{id} ")
	public Integer deleteTeacher(String id);
	
	@Update("delete from user_info where user_id=#{id} ")
	public Integer deleteUser(String id);
	
	@Select("select course_id, course_name from course_info")
	public List<Map<String, String>> getcourse();
	
	@Select("select "
			+ "count(*) as count "
			+ "from teacher_info as a inner join course_info as b "
			+ "on a.subject=b.course_id "
			+ "where "
			+ "(#{id}='' or a.tch_id=#{id}) "
			+ "and (#{subject}='' or a.subject=#{subject}) ")
	
	public List<Map<String, Object>> getCount( String subject,String id);
	
}